(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.dynaDisc.negotiateRateController', negotiateRateController);
	negotiateRateController.$inject = [ '$scope', '$filter', 'Constants', 'arithService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'dynaDiscService' ];
	function negotiateRateController($scope, $filter, Constants, arithService, modalService, gridService, layoutConfig, validatorConfig, authService, dynaDiscService) {
		var ctrl = this, piDiscountRequestInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.piDiscountRequest = {};

		ctrl.init = function() {
			ctrl.piDiscountRequest = piDiscountRequestInfo;
			ctrl.dateDisabled = dynaDiscService.dateDisabled;
			ctrl.rateTypeOptions = dynaDiscService.getRateTypeOptions();
		};
		
		

		ctrl.negotiateRatePreview = function() {
			ctrl.piDiscountRequest.invoiceIds = [];
			angular.forEach(ctrl.piDiscountRequest.discountInvoiceList, function(invoice, index) {
				ctrl.piDiscountRequest.invoiceIds.push($filter('encode')(invoice.invoiceId));
			});
			return authService.negotiateRatePreview(ctrl.piDiscountRequest).then(successful, failed);
		};

		function failed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function successful(response) {
			if (response.data && response.data.model) {
				var piDiscountRequestTemp = response.data.model;
				piDiscountRequestTemp.sellerCode = ctrl.piDiscountRequest.sellerCode;
				piDiscountRequestTemp.sellerName = ctrl.piDiscountRequest.sellerName;
				piDiscountRequestTemp.taskId = piDiscountRequestInfo.taskId;
				var params = {
						'selectedRows' : ctrl.selectedRows,
						'piDiscountRequest' : piDiscountRequestTemp
				};
				$scope.go('apf.dynaDisc.negotiatePreview',params);
			}
		}
		
		
	}
})(angular);